package com.woniu.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.commons.exception.YogaException;
import com.woniu.commons.model.Result;
import com.woniu.commons.util.JwtUtil;
import com.woniu.commons.util.RedisUtil;
import com.woniu.model.dto.LeagueClassDto;
import com.woniu.model.dto.MyCourseDto;
import com.woniu.model.form.LeagueClassAddForm;
import com.woniu.model.form.LeagueClassUpdateStateForm;
import com.woniu.model.form.PageForm;
import com.woniu.model.model.YLeagueClass;
import com.woniu.model.param.*;
import com.woniu.service.YLeagueClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@RestController
@RequestMapping("/yLeagueClass")
@Api(tags = "团课的接口")
public class YLeagueClassController {

    @Resource
    private YLeagueClassService leagueClassService;
    @Resource
    private RedisUtil redisUtil;


    /**
     * 修改团课状态为已完课：
     *
     * @param form：leagueClassId：团课id；leagueClassState：状态
     * @return
     * @throws YogaException
     */
    @PostMapping("/updateLeagueClassState")
    @ApiOperation("修改团课状态为已完课")
    public Result<?> updateLeagueClassState(@RequestBody LeagueClassUpdateStateForm form) throws YogaException {
        if (form.getLeagueClassId() == null || form.getLeagueClassState() == null) {
            return Result.newInstance(null).setMsg("参数异常").setCode(500);
        }
        leagueClassService.updateLeagueClassState(BeanUtil.toBean(form, LeagueClassUpdateStateParam.class));
        return Result.newInstance(null).setMsg("修改团课状态完成").setCode(200);
    }


    /**
     * 删除团课
     *
     * @param leagueClassId：团课id
     * @return
     * @throws YogaException
     */
    @GetMapping("/deleteLeagueClassState")
    @ApiOperation("删除团课")
    public Result<?> deleteLeagueClassState( Integer leagueClassId) throws YogaException {
        System.out.println("控制层的"+leagueClassId);
        if (leagueClassId == null) {
            return Result.newInstance(null).setMsg("参数异常").setCode(500);
        }
        leagueClassService.deleteLeagueClassState(leagueClassId);
        return Result.newInstance(null).setMsg("删除团课状态完成").setCode(200);
    }


    /**
     * 新增团课
     *
     * @param form：leagueClassName：团课名；coachId：教练名；peopleNum：课程容纳人数；venueId：场馆id；price：价格；minute：分钟数(课程时长)；classTypeId：课程类型id；leagueClassState：0.已取消1.可预约2.预约满3.已完课；leagueClassDetail：课程描述；classroomId：教室id；classNum：课程数量（节）
     * @return
     * @throws YogaException
     */
    @PostMapping("/addLeagueClass")
    @ApiOperation("新增团课")
    public Result<?> addLeagueClass(@RequestBody LeagueClassAddForm form,@RequestHeader String token) throws YogaException {
        String refreshToken = redisUtil.get(token);
        Integer venueId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("venueId").toString());

        LeagueClassAddParam param = BeanUtil.toBean(form, LeagueClassAddParam.class);
        param.setVenueId(venueId);
        leagueClassService.addLeagueClass(param);
        return Result.newInstance(null).setCode(200).setMsg("新增成功");
    }


    /**
     * 查询团课集合
     *
     * @return
     */
    @GetMapping("/loadListLeagueClass")
    @ApiOperation("查询团课集合")
    public Result<?> loadListLeagueClass() {
        return Result.newInstance(List.class).setCode(200).setMsg("查询成功").setData(leagueClassService.loadListLeagueClass());
    }


    /**
     * 查询团课集合,带团课名搜索条件查询
     * @return
     */
    @GetMapping("/loadListByLeagueClass")
    @ApiOperation("查询团课集合,带团课名搜索条件查询")
    public Result<?> loadListByLeagueClass(String LeagueClassName) {
        return Result.newInstance(List.class).setCode(200).setMsg("查询成功").setData(leagueClassService.loadListByLeagueClass(LeagueClassName));
    }

    /**
     * 通过团课Id查一个团课
     *
     * @param id：团课id
     * @return
     */
    @GetMapping("/ByIdLeagueClass")
    @ApiOperation("通过团课Id查一个团课")
    public Result<?> ByIdLeagueClass(Integer id) {
        YLeagueClass info = leagueClassService.ByIdLeagueClass(id);
        return Result.newInstance(YLeagueClass.class).setData(info).setMsg("查询成功").setCode(200);
    }



    /**
     * 通过教练Id查该教练的团课
     *
     * @param coachId：教练id
     * @return
     */
    @GetMapping("/CoachByListIdLeagueClass")
    @ApiOperation("通过教练Id查该教练的团课")
    public Result<?> CoachByIdLeagueClass(Integer coachId) {
        List list = leagueClassService.CoachByIdLeagueClass(coachId);
        return Result.newInstance(List.class).setData(list).setMsg("查询成功").setCode(200);
    }




    /**
     * 通过教练Id查该教练的团课详情
     *
     * ：教练id
     * @return
     */
    @GetMapping("/CoachLeagueClassMessage")
    @ApiOperation("通过教练Id查教练的团课详情")
    public Result<Page<MyCourseDto>> CoachLeagueClassMessage(@RequestHeader String token,PageForm form) {
        String refreshToken = redisUtil.get(token);
        Integer coachId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("id").toString());
        CoachMessageParam param = BeanUtil.toBean(form, CoachMessageParam.class);
        param.setCoachId(coachId);
        return Result.newPageInstance(MyCourseDto.class).setData(leagueClassService.CoachLeagueClassMessage(param)).setMsg("查询成功").setCode(200);
    }




    /**
     * 通过场馆Id查该场馆的团课
     *
     * @param venueId：场馆id
     * @return
     */
    @GetMapping("/CoachByIdLeagueClass")
    @ApiOperation("通过场馆Id查该教练的团课")
    public Result<?> VenueByIdLeagueClass(Integer venueId) {
        List list = leagueClassService.VenueByIdLeagueClass(venueId);
        return Result.newInstance(List.class).setData(list).setMsg("查询成功").setCode(200);
    }

    /**
     * 教练端分页查询所有团课
     * @param form
     * @return
     * @throws Exception
     */
    @GetMapping("/pageAllLeagueClass")
    @ApiOperation("教练端分页查询所有团课")
    public Result<Page<LeagueClassDto>> pageAllLeagueClass(PageForm form,@RequestHeader String token) throws YogaException {
        String refreshToken = redisUtil.get(token);
        Integer coachId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("id").toString());

        PageAllLeagueClassParam param = BeanUtil.toBean(form, PageAllLeagueClassParam.class);

        param.setCoachId(coachId);

        Page<LeagueClassDto> page = leagueClassService.pageAllLeagueClass(param);
        return Result.newPageInstance(LeagueClassDto.class).setMsg("查询成功").setData(page).setCode(200);
    }








    /**
     * 场馆端分页查询自己场馆的所有团课
     * @param form
     * @return
     * @throws Exception
     */
    @GetMapping("/VenuePageMyLeagueClass")
    @ApiOperation("场馆端分页查询自己场馆的所有团课")
    public Result<Page<LeagueClassDto>> VenuePageMyLeagueClass(PageForm form,@RequestHeader String token) throws YogaException {
        String refreshToken = redisUtil.get(token);
        Integer venueId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("venueId").toString());

        VenuePageLeagueClassParam param = BeanUtil.toBean(form, VenuePageLeagueClassParam.class);

        param.setVenueId(venueId);

        Page<LeagueClassDto> page = leagueClassService.VenuePageMyLeagueClass(param);
        return Result.newPageInstance(LeagueClassDto.class).setMsg("查询成功").setData(page).setCode(200);
    }

    /**
     * 团课上架
     * @param leagueClassId
     * @return
     * @throws Exception
     */
    @GetMapping("/VenuePutawayLeagueClass")
    @ApiOperation("团课上架")
    public Result<?> VenuePutawayLeagueClass(Integer leagueClassId,@RequestHeader String token) throws YogaException {
        String refreshToken = redisUtil.get(token);
        Integer venueId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("venueId").toString());

        leagueClassService.VenuePutawayLeagueClass(leagueClassId,venueId);
        return Result.getResult().setMsg("上架成功");
    }

    /**
     * 加载所有团课对象
     *
     * @return
     * @throws YogaException
     */
    @GetMapping("/loadAllLeagueClass")
    @ApiOperation("加载所有团课对象")
    public Result<List<YLeagueClass>> loadAllLeagueClass(){
        List<YLeagueClass> list = leagueClassService.loadAllLeagueClass();
        return Result.newListInstance(YLeagueClass.class).setData(list).setMsg("查询所有团课成功");
    }

    /**
     * 通过名称加载所有团课对象
     *
     * @return
     * @throws YogaException
     */
    @GetMapping("/loadLeagueClassByName")
    @ApiOperation("通过名称加载所有团课对象")
    public Result<List<YLeagueClass>> loadLeagueClassByName(String leagueClassName){
        List<YLeagueClass> list = leagueClassService.loadLeagueClassByName(leagueClassName);
        return Result.newListInstance(YLeagueClass.class).setData(list).setMsg("查询所有团课成功");
    }
}

